<?php

namespace app\modules\api\controllers;

use Yii;
use yii\web\Controller;
use common\models\Tools;
use \common\models\Works;

/**
 * Default controller for the `api` module
 */
class IndexController extends Controller
{
    public $enableCsrfValidation = false;
    
    
    /**
     * 初始化长连接时获取用户的相关数据
     * url /api/index/websocket-init
     */
    public function actionWebsocketInit(){
        //检测登录情况
        if (Yii::$app->user->isGuest) {
            $output['success'] = 0;
            $output['msg'] = "未登录";
            Tools::outputjson($output);
        }
        
        $cookie = \Yii::$app->request->cookies;
        
        $usercode = \common\models\Tools::getUserCode(Yii::$app->user->identity->created_at, Yii::$app->user->identity->id);
        
        $server = \common\models\WebsocketSever2user::getServerByUserid(Yii::$app->user->identity->id);
        if(empty($server)){
            $server = \common\models\WebsocketSever::distributeServer(Yii::$app->user->identity->id);
        }
        
        $data['server'] = $server;
        $data['usercode'] = $usercode;
        $data['userid'] = Yii::$app->user->identity->id;
        $data['username'] = Yii::$app->user->identity->username;
        $data['headimg'] = empty(Yii::$app->user->identity->headimg) ? '/img/user/default-head.png' : Yii::$app->user->identity->headimg;
        
        
        $output['success'] = 1;
        $output['msg'] = "请求成功";
        $output['data'] = $data;
        Tools::outputjson($output);
    }
    
    public function actionChecklogin(){
        //检测登录情况
        if (Yii::$app->user->isGuest) {
            $output['success'] = 0;
            $output['msg'] = "未登录";
            Tools::outputjson($output);
        }else{
            $output['success'] = 1;
            $output['msg'] = "已登录";
            Tools::outputjson($output);
        }
    }
}
